/**
 * 
 */
package com.alstudio.ui.base;

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.pojo.module.server.Serverlet;
import com.alstudio.ui.config.BaseTitleActivityConfig;
import com.alstudio.utils.android.ALFileManager;
import com.alstudio.utils.android.AndroidMultiMediaManager;
import com.alstudio.utils.android.imageProcess.ALImageProcessParams;
import com.alstudio.utils.android.net.http.UploadPhotoRequest;
import com.alstudio.utils.android.net.http.UploadPhotoRequest.Callback;
import com.alstudio.utils.android.net.uploader.ALHttpUploadTaskManager;
import com.alstudio.utils.log.ALLog;
import com.alstudio.view.listener.AnimationEndListener;
import com.loovee.imaohu.R;

/**
 * 本类为所有带状态栏的界面的父类。本界面带有一个 实现好的状态栏，内涵左按钮、有按钮、中间文字区。状态栏以下装在 传递进来的布局页面。
 * 
 * @author Alonso Lee
 * 
 */
public abstract class TitleBarActivity extends BaseActivity {

	// 状态栏布局
	protected RelativeLayout baseTitleBarLayout;
	// 内容布局
	private LinearLayout mContentLayout, titleBarLayout, customTitleBarLayout;
	// 左边按钮
	private Button leftButton;
	// 右边按钮
	private Button rightButton;
	// 左边image button
	private ImageButton leftImgButton;
	// 右边image button
	private ImageButton rightImgButton;
	// 自定义左按键区
	private LinearLayout mLeftCustomButton;
	// 自定义右按键区
	private LinearLayout mRightCustomButton;
	// 左边按钮类型
	private ButtonType mLeftButtonType = ButtonType.BUTTON_NORMAL;
	// 右边按钮类型
	private ButtonType mRightButtonType = ButtonType.BUTTON_NORMAL;

	// 中间文字信息
	private TextView center_msg;
	// 本类界面独有的通知区域。显示在状态栏下发
	private LinearLayout titleBarNoticeArea;

	// 状态栏通知区域事件监听器
	private NoticeAreaEventListener mTitleBarNoticeAreaListener;
	// 此变量用于标识当前是否正在显示状态栏通知区域
	private boolean isTitleBarNoticeAreaShowing = false;
	// 此变量用于标识是否需要定时自动关闭状态栏通知区域
	private boolean isAutoDismissTitleBarNoticeAreaEnable = true;
	// 状态栏通知区域缺省显示时常
	private long TITLE_BAR_NOTICE_AREA_SHOW_DURATION = 5 * 1000;
	// 状态栏通知区域显示时常
	private long mNoticeAreaShowDuration = TITLE_BAR_NOTICE_AREA_SHOW_DURATION;
	// 自定义状态栏通知区域显示动画资源
	private int mTitleBarNoticeAreaShowAnimationRes = -1;
	// 自定义状态栏通知区域隐藏动画资源
	private int mTitleBarNoticeAreaDismissAnimationRes = -1;
	// 状态栏通知区域的显示、隐藏动画
	private Animation titleBarNoticeAnimation;
	// 关闭状态栏通知区域
	private final int DISMISS_TITLE_BAR_NOTICE_AREA = 10011;

	private boolean isLeftButtonShow = true;
	private boolean isRightButtonShow = true;

	private boolean rightUseImageButton = false;
	private boolean leftUseImageButton = true;

	private ImageView txt_drawable;

	protected LinearLayout center_layout;
	/**
	 * 显示配置文件
	 */
	private BaseTitleActivityConfig mBaseConfig;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 初始化UI控件
		initWidget();
	}

	/***************************************************************** 控件显示配置管理 *************************************************************************/

	/******************************************************************************************** 控件处理 ****************************************************************************************/
	/**
	 * 初始化UI控件
	 * 
	 * @author Alonso Lee
	 */
	private void initWidget() {
		// 将布局文件加入到BaseActivity中
		setContentView(R.layout.base_title_bar_layout);

		// 状态栏布局
		titleBarLayout = (LinearLayout) findViewById(R.id.title_bar);
		// 缺省状态栏
		baseTitleBarLayout = (RelativeLayout) findViewById(R.id.base_title_bar);
		// 自定义状态栏
		customTitleBarLayout = (LinearLayout) findViewById(R.id.custom_title_bar);

		// 主内容区
		mContentLayout = (LinearLayout) findViewById(R.id.title_bar_content_layout);
		// 左按钮
		leftButton = (Button) findViewById(R.id.button_left);
		// 右按钮
		rightButton = (Button) findViewById(R.id.button_right);
		// 左边image 按钮
		leftImgButton = (ImageButton) findViewById(R.id.img_button_left);
		// 右边 image 按钮
		rightImgButton = (ImageButton) findViewById(R.id.img_button_right);
		// 左边自定义按键
		mLeftCustomButton = (LinearLayout) findViewById(R.id.custom_button_left);
		// 右边自定义按键
		mRightCustomButton = (LinearLayout) findViewById(R.id.custom_button_right);

		// 中间的布局
		center_layout = (LinearLayout) findViewById(R.id.center_layout);
		// 中间文字信息
		center_msg = (TextView) findViewById(R.id.center_msg);
		// 文字旁边的图标
		txt_drawable = (ImageView) findViewById(R.id.txt_drawable);
		// title bar下的通知区域
		titleBarNoticeArea = (LinearLayout) findViewById(R.id.title_bar_notice_area);
		// 设置缺省的点击事件
		setButtonDefaultClickListener();
	}

	/**
	 * 设置TietleBar标题栏下面内容区的布局文件
	 * 
	 * @param layoutResId
	 *            布局资源文件
	 * @author Alonso Lee
	 */
	protected void setTitleBarContentView(int layoutResId) {
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(layoutResId, null);
		if (v != null) {
			v.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			mContentLayout.addView(v);
		}
	}

	/**
	 * 设置TietleBar标题栏下面内容区的布局文件
	 * 
	 * @param layoutResId
	 *            布局资源文件
	 * @author Alonso Lee
	 */
	protected void setTitleBarContentView(View v) {
		if (v != null) {
			v.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			mContentLayout.addView(v);
		}
	}

	/**
	 * 设定左边按钮类型
	 * 
	 * @param type
	 */
	protected void setLeftButtonType(ButtonType type) {
		this.mLeftButtonType = type;
		updateButtonConfig(true);
	}

	/**
	 * 设定右边按钮类型
	 * 
	 * @param type
	 */
	protected void setRightButtonType(ButtonType type) {
		this.mRightButtonType = type;
		updateButtonConfig(false);
	}

	/**
	 * 获取左边按钮类型
	 * 
	 * @return
	 */
	protected ButtonType getLeftButtonType() {
		return this.mLeftButtonType;
	}

	/**
	 * 获取右边按钮类型
	 * 
	 * @return
	 */
	protected ButtonType getRightButtonType() {
		return this.mRightButtonType;
	}

	/**
	 * 配置显示界面
	 */
	private void prepareShowMode() {

		rightUseImageButton = mBaseConfig.isRightUseImageButtonRequest();

		leftUseImageButton = mBaseConfig.isleftUseImageButtonRequest();

		if (rightUseImageButton) {
			// 我要用image button
			rightButton.setVisibility(View.GONE);
			rightImgButton.setVisibility(View.VISIBLE);
		}

		if (leftUseImageButton) {
			leftButton.setVisibility(View.GONE);
			leftImgButton.setVisibility(View.VISIBLE);
		}

		if (mBaseConfig.isHideTitleBarRequest()) {
			hideTitleBar();
		}

		if (mBaseConfig.getCustomTitleBarRes() != BaseTitleActivityConfig.INVALID_RES_PARAM) {
			setCustomTitleBar(mBaseConfig.getCustomTitleBarRes());
		} else {
			if (mBaseConfig.isRightButtonHideRequest()) {
				hideRightButton(true);
			} else {
				if (mBaseConfig.getRightBtBgRes() != BaseTitleActivityConfig.INVALID_RES_PARAM) {
					setRightButtonBackground(mBaseConfig.getRightBtBgRes());
				}

				if (mBaseConfig.getRightImageBtSrc() != BaseTitleActivityConfig.INVALID_RES_PARAM) {
					setRightButtonImageSrc(mBaseConfig.getRightImageBtSrc());
				}

				if (!TextUtils.isEmpty(mBaseConfig.getRightBtLable())) {
					setRightButtonName(mBaseConfig.getRightBtLable());
				}
			}

			if (mBaseConfig.isLeftButtonHideRequest()) {
				hideLeftButton(true);
			} else {
				if (mBaseConfig.getLeftBtBgRes() != BaseTitleActivityConfig.INVALID_RES_PARAM) {
					setLeftButtonBackground(mBaseConfig.getLeftBtBgRes());
				}

				if (!TextUtils.isEmpty(mBaseConfig.getLeftBtLable())) {
					setLeftButtonName(mBaseConfig.getLeftBtLable());
				}
			}
			setCenterTextViewMessage(mBaseConfig.getTitle());
		}
		setButtonDefaultClickListener();
	}

	/**
	 * 设置配置文件
	 * 
	 * @param config
	 */
	protected void setConfig(BaseTitleActivityConfig config) {
		if (config == null) {
			throw new IllegalArgumentException(
					"come on，怎么不设置配置文件进来呀。不然我怎么知道如何显示撒");
		}
		this.mBaseConfig = config;
		prepareShowMode();
	}

	private View contentView;

	/**
	 * 设置TietleBar标题栏下面内容区的布局文件
	 * 
	 * @param layoutResId
	 *            布局资源文件
	 * @author Alonso Lee
	 */
	protected void setTitleBarActivityContentView(int layoutResId) {
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		contentView = inflater.inflate(layoutResId, null);
		if (contentView != null) {
			contentView.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			mContentLayout.addView(contentView);
		}
	}

	/**
	 * 根据传入的资源文件，设定自定义状态栏。缺省的状态栏将自动隐藏。
	 * 
	 * @param res
	 *            布局资源文件
	 */
	protected void setCustomTitleBar(int res) {

		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(res, null);
		if (v != null) {
			customTitleBarLayout.addView(v);
			baseTitleBarLayout.setVisibility(View.GONE);
		}
	}

	/**
	 * 将传入的view座位自定义状态栏。缺省的状态栏将自动隐藏。
	 * 
	 * @param v
	 */
	protected void setCustomTitleBar(View v) {

		if (v != null) {
			customTitleBarLayout.addView(v);
			baseTitleBarLayout.setVisibility(View.GONE);
		}
	}

	protected void hideTitleBar() {
		baseTitleBarLayout.setVisibility(View.GONE);
	}

	protected void showTitleBar() {
		baseTitleBarLayout.setVisibility(View.VISIBLE);
	}
	
	protected void invisiableTitleBar() {
		baseTitleBarLayout.setVisibility(View.INVISIBLE);
	}

	/**
	 * 获取左边按钮
	 * 
	 * @return 左边按钮实例
	 * @author Alonso Lee
	 */
	public View getLeftButton() {
		if (leftUseImageButton) {
			return leftImgButton;
		}
		return this.leftButton;
	}

	/**
	 * 获取右边按钮
	 * 
	 * @return 右边按钮实例
	 * @author Alonso Lee
	 */
	public View getRightButton() {
		if (rightUseImageButton) {
			return this.rightImgButton;
		}
		return this.rightButton;
	}

	/**
	 * 获取中间TextView对象
	 * 
	 * @return 中间TextView对象实例
	 * @author Alonso Lee
	 */
	public TextView getCenterTextView() {
		return this.center_msg;
	}

	/**
	 * 设定左边按钮文字信息
	 * 
	 * @param name
	 *            要显示的文字信息
	 * @author Alonso Lee
	 */
	public void setLeftButtonName(String name) {
		this.leftButton.setText(name);
	}

	public void setLeftButtonName(int res) {
		this.leftButton.setText(res);
	}

	/**
	 * 设置右边按钮文字信息
	 * 
	 * @param name
	 *            文字信息
	 * @author Alonso Lee
	 */
	public void setRightButtonName(String name) {
		this.rightButton.setText(name);
	}

	/**
	 * 设置右边按钮文字信息
	 * 
	 * @param name
	 *            文字信息
	 * @author Alonso Lee
	 */
	public void setRightButtonName(int res) {
		this.rightButton.setText(res);
	}

	/**
	 * 设定导航栏中间文字信息
	 * 
	 * @param msg
	 * @author Alonso Lee
	 */
	public void setCenterTextViewMessage(String msg) {
		this.center_msg.setText(msg);
	}

	/**
	 * 设定导航栏中间文字信息
	 * 
	 * @param res
	 *            文字资源
	 */
	public void setCenterTextViewMessage(int res) {
		this.center_msg.setText(res);
	}

	/**
	 * 设置中间文字旁边的图片
	 * 
	 * @param res
	 * @param direction
	 */
	public void setCenterMsgDrawable(int res, int direction) {
		if (direction == NONE) {
			goneView(txt_drawable);
			return;
		}
		showView(txt_drawable);
		txt_drawable.setImageResource(res);
	}

	/**
	 * 设定导航栏左边按钮资源图片
	 * 
	 * @param resId
	 *            资源图片id
	 * @author Alonso Lee
	 */
	public void setLeftButtonBackground(int resid) {
		if (rightUseImageButton) {
			this.leftImgButton.setBackgroundResource(resid);
		} else {
			this.leftButton.setBackgroundResource(resid);
		}
	}

	/**
	 * 设定导航栏右边按钮资源图片
	 * 
	 * @param resid
	 *            资源图片id
	 * @author Alonso Lee
	 */
	public void setRightButtonBackground(int resid) {
		if (rightUseImageButton) {
			this.rightImgButton.setBackgroundResource(resid);
		} else {
			this.rightButton.setBackgroundResource(resid);
		}
	}

	/**
	 * 设定导航栏左边按钮文字颜色
	 * 
	 * @param color
	 *            文字颜色
	 * @author Alonso Lee
	 */
	public void setLeftButtonTextColor(int color) {
		if (!leftUseImageButton) {
			this.leftButton.setTextColor(color);
		}
	}

	/**
	 * 设定导航栏右边按钮文字颜色
	 * 
	 * @param color
	 *            文字颜色
	 * @author Alonso Lee
	 */
	public void setRightButtonTextColor(int color) {
		if (!rightUseImageButton) {
			this.rightButton.setTextColor(color);
		}
	}

	public void setLeftButtonImageSrc(int src) {
		if (leftUseImageButton) {
			this.leftImgButton.setImageResource(src);
		}
	}

	public void setRightButtonImageSrc(int src) {
		if (rightUseImageButton) {
			this.rightImgButton.setImageResource(src);
		}
	}

	/**
	 * 设定导航栏中间文字颜色
	 * 
	 * @param color
	 * @author Alonso Lee
	 */
	public void setCenterTextViewTextColor(int color) {
		this.center_msg.setTextColor(color);
	}

	/**
	 * 中间布局点击事件
	 * 
	 * @param listener
	 */
	public void setCenterLayoutClickListener(OnClickListener listener) {
		center_layout.setOnClickListener(listener);
	}

	/**
	 * 设定左边按钮显示状态
	 * 
	 * @param hide
	 *            true 表示隐藏左边按钮，false表示显示左边按钮
	 * @author Alonso Lee
	 */
	public void hideLeftButton(boolean hide) {

		if (hide) {
			if (leftUseImageButton) {
				leftImgButton.setVisibility(View.INVISIBLE);
			} else {
				leftButton.setVisibility(View.INVISIBLE);
			}
		} else {
			if (leftUseImageButton) {
				leftImgButton.setVisibility(View.VISIBLE);
			} else {
				leftButton.setVisibility(View.VISIBLE);
			}
		}

	}

	/**
	 * 测试左边按钮是否已经被隐藏
	 * 
	 * @return true 表示左边按钮已经被隐藏，false表示左边按钮当前处于可见状态
	 * @author Alonso Lee
	 */
	public boolean isLeftButtonHidden() {
		int state = -1;
		if (leftUseImageButton) {
			state = leftImgButton.getVisibility();
		} else {
			state = leftButton.getVisibility();
		}
		switch (state) {
		case View.VISIBLE:
			return false;
		case View.GONE:
		case View.INVISIBLE:
			return true;
		default:
			return false;
		}

	}

	/**
	 * 设定右边按钮显示状态
	 * 
	 * @param hide
	 *            true 表示隐藏右边按钮，false表示显示右边按钮
	 * @author Alonso Lee
	 */
	public void hideRightButton(boolean hide) {
		if (hide) {
			rightImgButton.setVisibility(View.INVISIBLE);
			rightButton.setVisibility(View.INVISIBLE);

		} else {
			if (rightUseImageButton) {
				rightImgButton.setVisibility(View.VISIBLE);
			} else {
				rightButton.setVisibility(View.VISIBLE);
			}
		}
	}

	/**
	 * 测试右边按钮是否已经被隐藏
	 * 
	 * @return true 表示右边按钮已经被隐藏，false表示右边按钮当前处于可见状态
	 * @author Alonso Lee
	 */
	public boolean isRightButtonHidden() {
		int state = -1;
		if (rightUseImageButton) {
			state = rightImgButton.getVisibility();
		} else {
			state = rightButton.getVisibility();
		}
		switch (state) {
		case View.VISIBLE:
			return false;
		case View.GONE:
		case View.INVISIBLE:
			return true;
		default:
			return false;
		}
	}

	/**
	 * 设定状态栏左边按钮事件监听器
	 * 
	 * @param listener
	 * @author Alonso Lee
	 */
	public void setLeftButtonClickListener(View.OnClickListener listener) {
		// if (leftUseImageButton) {
		// this.leftImgButton.setOnClickListener(listener);
		// } else {
		// this.leftButton.setOnClickListener(listener);
		// }
		this.leftImgButton.setOnClickListener(listener);
		this.leftButton.setOnClickListener(listener);
	}

	/**
	 * 设定状态栏右边按钮事件监听器
	 * 
	 * @param listener
	 * @author Alonso Lee
	 */
	public void setRightButtonClickListener(View.OnClickListener listener) {
		// if (rightUseImageButton) {
		// this.rightImgButton.setOnClickListener(listener);
		// } else {
		// this.rightButton.setOnClickListener(listener);
		// }
		this.rightImgButton.setOnClickListener(listener);
		this.rightButton.setOnClickListener(listener);

	}

	public void rightUseImageButton(boolean image) {
		this.rightUseImageButton = image;
		if (image) {
			rightImgButton.setVisibility(View.VISIBLE);
			rightButton.setVisibility(View.GONE);
		} else {
			rightButton.setVisibility(View.VISIBLE);
			rightImgButton.setVisibility(View.GONE);
		}
	}

	public boolean isUseRightImageButtonRequest() {
		return this.rightUseImageButton;
	}

	public void leftUseImageButton(boolean image) {
		this.leftUseImageButton = image;
		if (image) {
			leftImgButton.setVisibility(View.VISIBLE);
			leftButton.setVisibility(View.GONE);
		} else {
			leftButton.setVisibility(View.VISIBLE);
			leftImgButton.setVisibility(View.GONE);
		}
	}

	public boolean isUseLeftImageButtonRequest() {
		return this.leftUseImageButton;
	}

	/**
	 * 设定状态栏通知区域view事件监听器
	 * 
	 * @param listener
	 * @author Alonso Lee
	 */
	protected void setTitleBarNoticeAreaListener(
			NoticeAreaEventListener listener) {
		if (listener != null) {
			mTitleBarNoticeAreaListener = listener;
		}
	}

	/**
	 * 移除状态栏通知区域view事件监听器
	 * 
	 * @author Alonso Lee
	 */
	protected void removeTitleBarNoticeAreaListener() {
		mTitleBarNoticeAreaListener = null;
	}

	/**
	 * 将制定的view添加到状态栏通知区域中。
	 * 
	 * @param child
	 * @author Alonso Lee
	 */
	protected void setTitleBarNoticeLayout(View child) {

		if ((titleBarNoticeArea != null) && (child != null)) {
			titleBarNoticeArea.removeAllViews();
			child.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			titleBarNoticeArea.addView(child);

			titleBarNoticeArea.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					dismissTitleBarNotice();
				}
			});
		}
	}

	/**
	 * 将制定的view添加到状态栏通知区域中。
	 * 
	 * @param child
	 * @author Alonso Lee
	 */
	protected void setTitleBarNoticeLayout(View child, boolean fillParent) {

		if ((titleBarNoticeArea != null) && (child != null)) {
			titleBarNoticeArea.removeAllViews();
			if (fillParent) {
				child.setLayoutParams(new LinearLayout.LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

			} else {
				child.setLayoutParams(new LinearLayout.LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

			}
			titleBarNoticeArea.addView(child);
		}
	}

	/**
	 * 设定状态栏通知区域显示时常。传入的值为毫秒。
	 * 
	 * @param time
	 * @author Alonso Lee
	 */
	protected void setTitleBarNoticeAreaShowDuration(long ms) {
		if (ms > 0) {
			mNoticeAreaShowDuration = ms;
		}
	}

	/**
	 * 设定状态栏通知区域显示时的过场动画
	 * 
	 * @param res
	 *            -1则表示恢复缺省动画
	 * @author Alonso Lee
	 */
	protected void setTitleBarNoticeAreaShowAnimationRes(int res) {
		mTitleBarNoticeAreaShowAnimationRes = res;
	}

	/**
	 * 设定状态栏通知区域隐藏时的过场动画
	 * 
	 * @param res
	 *            -1则表示恢复缺省动画
	 * @author Alonso Lee
	 */
	protected void setTitleBarNoticeAreaDismissAnimationRes(int res) {
		mTitleBarNoticeAreaDismissAnimationRes = res;
	}

	/**
	 * 设定状态栏通知区域是否定时自动消失
	 * 
	 * @param enable
	 *            true表示需要自动消失 false 表示不需要自动消失
	 * @author Alonso Lee
	 */
	protected void setTitleBarNoticeAreaAutoDismiss(boolean enable) {
		isAutoDismissTitleBarNoticeAreaEnable = enable;
	}

	/**
	 * 检查状态栏通知区域当前是否设为定时自动消失
	 * 
	 * @return true 表示当前设定为自动消失
	 * @author Alonso Lee
	 */
	protected boolean isTitleBarNoticeAreaAutoDismiss() {
		return isAutoDismissTitleBarNoticeAreaEnable;
	}

	protected boolean isTitleBarNoticeAreaShowing() {
		return isTitleBarNoticeAreaShowing;
	}

	/**
	 * 显示状态栏通知区域
	 * 
	 * @author Alonso Lee
	 */
	protected void showTitleBarNotice() {
		if (titleBarNoticeArea != null) {
			titleBarNoticeArea.setVisibility(View.VISIBLE);
			isTitleBarNoticeAreaShowing = true;
			if (mTitleBarNoticeAreaShowAnimationRes != -1) {
				// 设定自定义显示动画
				titleBarNoticeAnimation = AnimationUtils.loadAnimation(this,
						mTitleBarNoticeAreaShowAnimationRes);
			} else {
				// 使用缺省显示动画
				titleBarNoticeAnimation = AnimationUtils.loadAnimation(this,
						R.anim.activity_down);
			}
			ALLog.d("有多少个" + titleBarNoticeArea.getChildCount());
			titleBarNoticeAnimation
					.setAnimationListener(titleBarNoticeAreaAnimationListener);
			titleBarNoticeArea.startAnimation(titleBarNoticeAnimation);
		}
	}

	/**
	 * 隐藏状态栏通知区域
	 * 
	 * @author Alonso Lee
	 */
	protected void dismissTitleBarNotice() {

		if (titleBarNoticeArea != null) {

			if (!isTitleBarNoticeAreaShowing) {
				return;
			}

			titleBarNoticeAreaHandler
					.removeMessages(DISMISS_TITLE_BAR_NOTICE_AREA);
			isTitleBarNoticeAreaShowing = false;

			if (mTitleBarNoticeAreaDismissAnimationRes != -1) {
				// 设定自定义消失动画
				titleBarNoticeAnimation = AnimationUtils.loadAnimation(this,
						mTitleBarNoticeAreaDismissAnimationRes);
			} else {
				// 使用缺省显示动画
				titleBarNoticeAnimation = AnimationUtils.loadAnimation(this,
						R.anim.activity_up);
			}
			titleBarNoticeAnimation
					.setAnimationListener(titleBarNoticeAreaAnimationListener);
			titleBarNoticeArea.startAnimation(titleBarNoticeAnimation);
		}
	}

	/**
	 * 此handler用于控制定时关闭状态栏通知区域
	 */
	private Handler titleBarNoticeAreaHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			dismissTitleBarNotice();
		};
	};

	/**
	 * 监听状态栏通知区域动画结束
	 */
	private AnimationListener titleBarNoticeAreaAnimationListener = new AnimationEndListener() {

		@Override
		public void onAnimationEnd(Animation animation) {
			if (isTitleBarNoticeAreaShowing) {
				ALLog.d("显示了");
				// 回调通知，已经正常显示了
				if (mTitleBarNoticeAreaListener != null) {
					mTitleBarNoticeAreaListener.onShow();
				}
				// 显示通知区域动画显示完毕，如果开启了定时自动关闭通知区域，那么延时通过handler实现
				if (isAutoDismissTitleBarNoticeAreaEnable) {
					titleBarNoticeAreaHandler.sendEmptyMessageDelayed(
							DISMISS_TITLE_BAR_NOTICE_AREA,
							mNoticeAreaShowDuration);
				}

			} else {
				// 隐藏通知区域
				titleBarNoticeArea.setVisibility(View.GONE);
				// 回调通知，已经隐藏了
				if (mTitleBarNoticeAreaListener != null) {
					mTitleBarNoticeAreaListener.onDismiss();
				}
			}
		}
	};

	/************************************************************************************* 内部类以及私有方法 ******************************************************************************************************************************************************************************************************************************************/

	private void updateButtonConfig(boolean left) {
		if (left) {
			// 配置左键

			switch (mLeftButtonType) {
			case BUTTON_NORMAL:
				// 显示Button
				leftButton.setVisibility(View.VISIBLE);
				leftImgButton.setVisibility(View.GONE);
				mLeftCustomButton.setVisibility(View.GONE);
				break;
			case IMAGE_BUTTON_NORMAL:
				// 显示ImageButton
				leftImgButton.setVisibility(View.VISIBLE);
				leftButton.setVisibility(View.GONE);
				mLeftCustomButton.setVisibility(View.GONE);
				break;
			case CUSTOM_BUTTON:
				// 显示自定义按键
				mLeftCustomButton.setVisibility(View.VISIBLE);
				leftImgButton.setVisibility(View.GONE);
				leftButton.setVisibility(View.GONE);
				break;
			}

		} else {
			// 配置右键

			switch (mRightButtonType) {
			case BUTTON_NORMAL:
				// 显示Button
				rightButton.setVisibility(View.VISIBLE);
				rightImgButton.setVisibility(View.GONE);
				mRightCustomButton.setVisibility(View.GONE);
				break;
			case IMAGE_BUTTON_NORMAL:
				// 显示ImageButton
				rightImgButton.setVisibility(View.VISIBLE);
				rightButton.setVisibility(View.GONE);
				mRightCustomButton.setVisibility(View.GONE);
				break;
			case CUSTOM_BUTTON:
				// 显示自定义按键
				mRightCustomButton.setVisibility(View.VISIBLE);
				rightImgButton.setVisibility(View.GONE);
				rightButton.setVisibility(View.GONE);
				break;
			}
		}
	}

	private void setButtonBackground(boolean left, int res) {
		if (left) {
			switch (mLeftButtonType) {
			case BUTTON_NORMAL:
				this.leftButton.setBackgroundResource(res);
				break;
			case IMAGE_BUTTON_NORMAL:
				this.leftImgButton.setBackgroundResource(res);
				break;
			case CUSTOM_BUTTON:
				this.mLeftCustomButton.setBackgroundResource(res);
				break;
			}
		} else {
			switch (mRightButtonType) {
			case BUTTON_NORMAL:
				this.rightButton.setBackgroundResource(res);
				break;
			case IMAGE_BUTTON_NORMAL:
				this.rightImgButton.setBackgroundResource(res);
				break;
			case CUSTOM_BUTTON:
				this.mRightCustomButton.setBackgroundResource(res);
				break;
			}
		}
	}

	private void setButtonBackground(boolean left, Drawable background) {
		if (left) {
			switch (mLeftButtonType) {
			case BUTTON_NORMAL:
				this.leftButton.setBackgroundDrawable(background);
				break;
			case IMAGE_BUTTON_NORMAL:
				this.leftImgButton.setBackgroundDrawable(background);
				break;
			case CUSTOM_BUTTON:
				this.mLeftCustomButton.setBackgroundDrawable(background);
				break;
			}
		} else {
			switch (mRightButtonType) {
			case BUTTON_NORMAL:
				this.rightButton.setBackgroundDrawable(background);
				break;
			case IMAGE_BUTTON_NORMAL:
				this.rightImgButton.setBackgroundDrawable(background);
				break;
			case CUSTOM_BUTTON:
				this.mRightCustomButton.setBackgroundDrawable(background);
				break;
			}
		}
	}

	private void setButtonDefaultClickListener() {
		setLeftButtonClickListener(this);
		setRightButtonClickListener(this);
	}

	/******************************** 以下为拍照、选择照片、截取照片定义 **********************************************/
	private ALImageProcessParams mPhotoProcessParam;
	// 选择联系人照片
	private final int REQUEST_PHONE_CONTACT = 1024;

	/**
	 * 弹出选择拍照、选择图片对话框
	 */
	protected void showPhotoProcessDialog() {
		mPhotoProcessParam = new ALImageProcessParams();
		mPhotoProcessParam.setRequestHeight(640);
		mPhotoProcessParam.setRequestWidth(640);
		mPhotoProcessParam.setExplicitCrop(true);
		mPhotoProcessParam
				.setSavePath(AndroidMultiMediaManager.TEMP_TAKE_PHOTO_FILE_PATH);
		AndroidMultiMediaManager.showPhotoProcessDialog(this, false,
				mPhotoProcessParam);
	}

	/**
	 * 弹出选择拍照、选择图片对话框
	 */
	protected void showPhotoProcessDialog(boolean explicitCrop) {
		mPhotoProcessParam = new ALImageProcessParams();
		mPhotoProcessParam.setRequestHeight(640);
		mPhotoProcessParam.setRequestWidth(640);
		mPhotoProcessParam.setExplicitCrop(explicitCrop);
		mPhotoProcessParam
				.setSavePath(AndroidMultiMediaManager.TEMP_TAKE_PHOTO_FILE_PATH);
		AndroidMultiMediaManager.showPhotoProcessDialog(this, false,
				mPhotoProcessParam);
	}

	/**
	 * 执行上传图片到媒体服务器
	 * 
	 * @param filepath
	 */
	protected void uploadPic(String filepath) {
		// String servletUrl = "http://"
		// + ALLocalEnv.getLocalEnv().getVersionConfig().getServerInfo()
		// .getMediaServerIp()
		// + ":"
		// + ALLocalEnv.getLocalEnv().getVersionConfig().getServerInfo()
		// .getMediaServerPort() + Serverlet.UPLOAD_PIC_SERVERLET;

		String servletUrl = Serverlet
				.getUploadServerUrl(Serverlet.UPLOAD_PIC_SERVERLET);
		ALLog.d("上传url" + servletUrl + " 路径 " + filepath);
		ALHttpUploadTaskManager.UploadPhotoRequest(servletUrl, filepath,
				uploadCallback);
	}

	private Callback uploadCallback = new Callback() {

		@Override
		public void onSuccess(UploadPhotoRequest sender, String pictureFileId,
				String thumbFileId) {
			// TODO Auto-generated method stub
			handleUploadPicSuccess(sender, pictureFileId, thumbFileId);
		}

		@Override
		public void onFailed(UploadPhotoRequest sender, int errCode) {
			// TODO Auto-generated method stub
			handleUploadPicFail(sender, errCode);
		}

		@Override
		public void onCancelled(UploadPhotoRequest sender) {
			// TODO Auto-generated method stub

		}
	};

	/**
	 * 处理上传头像成功
	 * 
	 * @param sender
	 * @param pictureFileId
	 * @param thumbFileId
	 */
	protected void handleUploadPicSuccess(UploadPhotoRequest sender,
			String pictureFileId, String thumbFileId) {

	}

	/**
	 * 处理上传头像失败事件
	 * 
	 * @param sender
	 * @param errCode
	 */
	protected void handleUploadPicFail(UploadPhotoRequest sender, int errCode) {

	}

	/********************************************************* 以下选择照片、拍照、截取照片、选择联系人的处理以及回调事件 ***************************************************************************/
	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, intent);
		if (resultCode != RESULT_OK) {
			return;
		}
		switch (requestCode) {
		case AndroidMultiMediaManager.ACTION_TAKE_PHOTOS:
			// 拍照返回
			processOnTakePhotosResult(intent);
			break;
		case AndroidMultiMediaManager.ACTION_SELECT_PHOTOS:
			// 选择照片
			processOnSelectPhotosResult(intent);
			break;
		case AndroidMultiMediaManager.ACTION_CROP_PHOTOS:
			// 裁剪图片
			processOnCropPhotosResult(intent);
			break;
		case REQUEST_PHONE_CONTACT:
			// 选择联系人
			proccessPhoneContactsResult(intent);
			break;
		}
	}

	private void processOnTakePhotosResult(Intent result) {
		if (mPhotoProcessParam != null) {

			String imagePath = AndroidMultiMediaManager.getImagePath();
			if (TextUtils.isEmpty(imagePath)) {
				ALLog.d("拍照得到的图片路径有误");
				onGetProcessPhotosResult(imagePath, null);
				return;
			}

			if (mPhotoProcessParam.getCropRequest()) {
				ALLog.d("setting crop image request for image: " + imagePath);
				mPhotoProcessParam.setImagePath(imagePath);
				// 显示裁剪
				if (mPhotoProcessParam.isExplicitCrop()) {
					AndroidMultiMediaManager.dispatchCropPhotos(this,
							mPhotoProcessParam);
				} else {
					// 隐式裁剪
					String path = AndroidMultiMediaManager
							.cropPhotosInExplicitCrop(this, mPhotoProcessParam);
					String name = getFileName(path);

					onGetProcessPhotosResult(path, name);

				}
			} else {
				String name = getFileName(imagePath);

				onGetProcessPhotosResult(imagePath, name);
			}
		}

		reSetPhotoProcessParam();
	}

	private void processOnSelectPhotosResult(Intent result) {

		Uri uri = result.getData();
		if (uri != null) {
			String filePath;
			if (ALFileManager.getFilePathByUri(ALLocalEnv.getLocalEnv(), uri) != null) {
				filePath = ALFileManager.getFilePathByUri(
						ALLocalEnv.getLocalEnv(), uri);
			} else {
				filePath = uri.getPath();
			}

			if (mPhotoProcessParam != null) {

				if (TextUtils.isEmpty(filePath)) {
					ALLog.d("选择得到的图片路径有误");
					onGetProcessPhotosResult(null, null);
					return;
				}

				if (mPhotoProcessParam.getCropRequest()) {
					ALLog.d("setting crop image request for image: " + filePath);
					mPhotoProcessParam.setImagePath(filePath);

					// 显示裁剪
					if (mPhotoProcessParam.isExplicitCrop()) {
						AndroidMultiMediaManager.dispatchCropPhotos(this,
								mPhotoProcessParam);
					} else {
						// 隐式裁剪
						String path = AndroidMultiMediaManager
								.cropPhotosInExplicitCrop(this,
										mPhotoProcessParam);
						String name = getFileName(path);

						onGetProcessPhotosResult(path, name);
					}

				} else {
					String name = getFileName(filePath);
					if (!TextUtils.isEmpty(mPhotoProcessParam.getSavePath())) {

						File src = new File(filePath);
						File dstDir = new File(mPhotoProcessParam.getSavePath());

						if (!dstDir.exists()) {
							dstDir.mkdirs();
						}
						File dst = new File(mPhotoProcessParam.getSavePath()
								+ name);

						try {
							ALFileManager.copyFile(src, dst);
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
					filePath = mPhotoProcessParam.getSavePath() + name;

					onGetProcessPhotosResult(filePath, name);
				}
			}
		} else {
			onGetProcessPhotosResult(null, null);
		}

		reSetPhotoProcessParam();
	}

	private void processOnCropPhotosResult(Intent result) {
		reSetPhotoProcessParam();

		String path = null;
		String name = null;

		if (result != null) {
			path = result.getAction();
			name = getFileName(path);
			ALLog.d("得到裁剪后的图片路径: " + path);
		}

		onGetProcessPhotosResult(path, name);
	}

	private String getFileName(final String filePath) {
		String name = null;

		if (!TextUtils.isEmpty(filePath)) {
			int lastSeparatorIndex = -1;
			lastSeparatorIndex = filePath.lastIndexOf(File.separator);

			if (lastSeparatorIndex != -1) {
				name = filePath.substring(lastSeparatorIndex + 1);
			}
		}

		return name;
	}

	public void takePhotos(ALImageProcessParams param) {
		mPhotoProcessParam = param;
		AndroidMultiMediaManager.dispatchTakePictureIntent(this);
	}

	public void selectPhotos(ALImageProcessParams param) {
		mPhotoProcessParam = param;
		AndroidMultiMediaManager.dispatchSelectPhotos(this);
	}

	public void cropPhotos(ALImageProcessParams param) {
		mPhotoProcessParam = param;
		AndroidMultiMediaManager.dispatchCropPhotos(this, param);
	}

	protected void reSetPhotoProcessParam() {
		mPhotoProcessParam = null;
	}

	protected void onGetProcessPhotosResult(final String path, final String name) {

	}

	protected void onGetPhoneContactsResult(final String name,
			final String phone) {

	}

	private void proccessPhoneContactsResult(Intent data) {

		if (data == null) {
			return;
		}
		Uri contactData = data.getData();
		if (contactData == null) {
			return;
		}
		Cursor cursor = managedQuery(contactData, null, null, null, null);
		String name = "";
		String phoneNumber = "";
		if (cursor.moveToFirst()) {
			name = cursor.getString(cursor
					.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
			String hasPhone = cursor
					.getString(cursor
							.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
			String id = cursor.getString(cursor
					.getColumnIndex(ContactsContract.Contacts._ID));
			if (hasPhone.equalsIgnoreCase("1")) {
				hasPhone = "true";
			} else {
				hasPhone = "false";
			}
			if (Boolean.parseBoolean(hasPhone)) {
				Cursor phones = getContentResolver().query(
						ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
						null,
						ContactsContract.CommonDataKinds.Phone.CONTACT_ID
								+ " = " + id, null, null);
				while (phones.moveToNext()) {
					phoneNumber = phones
							.getString(phones
									.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
					setTitle(phoneNumber);
				}
				phones.close();
			}

			if (Integer.parseInt(Build.VERSION.SDK) < 14) {
				cursor.close();
			}
		}
		ALLog.d("名字 " + name + "电话 " + phoneNumber);
		onGetPhoneContactsResult(name, phoneNumber);
	}

	/**
	 * 描述按键类型
	 * 
	 * @author Alonso Lee
	 * 
	 */
	protected enum ButtonType {
		// 普通Button按键
		BUTTON_NORMAL,
		// 普通ImageButton按键
		IMAGE_BUTTON_NORMAL,
		// 自定义按键
		CUSTOM_BUTTON,
	}

	private long clickStartTime;
	private long lastClickTime;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button_left:
		case R.id.img_button_left:
			finish();
			break;
		case R.id.button_right:
		case R.id.img_button_right:
			invokeRightBtnClick();
			break;
		}
	}

	protected void invokeRightBtnClick() {

	}

}
